//
//  QDCustomToastContentView.m
//  qmuidemo
//
//  Created by zhoonchen on 2016/12/13.
//  Copyright © 2016年 QMUI Team. All rights reserved.
//

#import "QDCustomToastContentView.h"


@interface QDCustomToastContentView()
@property (nonatomic,assign)CGFloat width;
@end

@implementation QDCustomToastContentView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {
    _textLabel = [[UILabel alloc] init];
    self.textLabel.textColor = UIColorWhite;
    self.textLabel.font = UIFontBoldMake(14);
    self.textLabel.opaque = NO;
    [self addSubview:self.textLabel];
}

- (void)renderWithText:(NSString *)text{
    self.textLabel.text = text;
    [self.textLabel sizeToFit];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}


- (CGSize)sizeThatFits:(CGSize)size {
    
    CGFloat width = fmin(size.width, [QMUIHelper screenSizeFor55Inch].width);
    
    CGFloat height = 40;
    
    self.width = width;
    
    return CGSizeMake(fmin(size.width, width), fmin(size.height, height));
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat labWidth = [NSString textWidth:self.textLabel.text font:14 height:CGRectGetHeight(self.textLabel.bounds)];
    
    CGFloat labX = fmin(self.width, [QMUIHelper screenSizeFor55Inch].width)/2.0 - labWidth/2.0;

    CGFloat labY = 10;

    self.textLabel.frame = CGRectFlatMake(labX,labY,labWidth,CGRectGetHeight(self.textLabel.bounds));

}

@end
